package com.techstar.ats;

import lombok.RequiredArgsConstructor;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import com.techstar.ats.netty.AtsNettyClient;
import com.techstar.ats.service.AtsChannelFactory;

@Configuration
@EnableScheduling
@RequiredArgsConstructor
public class SchedConfiguration {
	private final AtsChannelFactory channelFactory;

	@Scheduled(cron = "0 0/1 * * * ?")
	public void channels() {
		channelFactory.removeTerminated();
		channelFactory.properties.forEach((line, connect) -> {
			AtsNettyClient client = channelFactory.getNettyBean(line);
			if(!client.isRunning()) {
				client.startAsync();
			}
		});
	}
}
